package com.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.persistence.Player;
import com.persistence.Reserve;
import com.persistence.TCaptain;
import com.persistence.TFootballTeam;
import com.persistence.TPhoto;
import com.persistence.TPlayer;
import com.service.CaptainService;
import com.service.PlayerService;
import com.service.TeamService;

@Controller
@SessionAttributes
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@Transactional
public class ContactController extends AbstractController {
	ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
	TeamService teamDao = (TeamService) context.getBean("teamService");
	PlayerService playerDao = (PlayerService) context.getBean("playerService");
	CaptainService captainDao = (CaptainService) context.getBean("captainService");

	@Override
	@RequestMapping(value = "/main", method = RequestMethod.GET)
	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		String module = req.getParameter("module");
		String teamId = req.getParameter("teamid");
		String action = req.getParameter("action");
		String playerId = req.getParameter("playerid");

		if(module==null){
			List<TFootballTeam> list = teamDao.findAll();
			return new ModelAndView("menu", "teams", list);
		}

		if(module.equalsIgnoreCase("")) {
			List<TFootballTeam> list = teamDao.findAll();
			return new ModelAndView("menu", "teams", list);
		}else if(module.equalsIgnoreCase("player")) {
			if(action.equalsIgnoreCase("add")) {
				List<TFootballTeam> list2 = teamDao.findAll();
				TFootballTeam team = teamDao.findById(Integer.parseInt(teamId));
				Map<String, Object> list3 = new HashMap<String, Object>();


				list3.put("team", list2);
				list3.put("team2", team);

				return new ModelAndView("addPlayer", "list3", list3);
			}else if(action.equalsIgnoreCase("del")) {
				TPlayer player = playerDao.findById(Integer.parseInt(playerId));
				playerDao.remove(player);

				List<TFootballTeam> list2 = teamDao.findAll();
				Map<String, Object> list3 = new HashMap<String, Object>();
				TFootballTeam team = teamDao.findById(Integer.parseInt(teamId));
				List<TPlayer> playerList = playerDao.findByProperty("TFootballTeam", team, 0);

				Iterator i = playerList.iterator();
				List<Player> players = new ArrayList<Player>();
				List<Reserve> reserves = new ArrayList<Reserve>();

				while(i.hasNext()) {
					TPlayer tp = (TPlayer) i.next();
					if(tp instanceof Player){
						players.add((Player)tp);
					}else if(tp instanceof Reserve){
						reserves.add((Reserve)tp);
					}
				}

				list3.put("team", list2);
				list3.put("player", players);
				list3.put("reserve", reserves);
				list3.put("team2", team);

				res.sendRedirect("main.htm?module=team&action=view&teamid=" + teamId);
				return null;



			}else if(action.equalsIgnoreCase("detail")) {
				List<TFootballTeam> list2 = teamDao.findAll();
				TPlayer player = playerDao.findById(Integer.parseInt(playerId));
				Map<String, Object> list3 = new HashMap<String, Object>();
				TFootballTeam team = teamDao.findById(Integer.parseInt(teamId));
				if(player instanceof Player){
					list3.put("status", "player");
				}else{
					list3.put("status", "reserve");
				}
				list3.put("team", list2);
				list3.put("team2", team);
				list3.put("player", player);

				return new ModelAndView("playerDetail", "list3", list3);
			}else if(action.equalsIgnoreCase("edit")) {
				List<TFootballTeam> list2 = teamDao.findAll();
				TPlayer player1 = playerDao.findById(Integer.parseInt(playerId));
				Map<String, Object> list3 = new HashMap<String, Object>();
				TFootballTeam team = teamDao.findById(Integer.parseInt(teamId));
				if(player1 instanceof Player){
					Player player = (Player) player1;
					TPhoto photo = player.getTPhoto();
					String foto = photo.getData().toString();
					list3.put("photo", foto);
					list3.put("status", "player");
					list3.put("player", player);
				}else{
					Reserve player = (Reserve) player1;
					TPhoto photo = player.getTPhoto();
					String foto = photo.getData().toString();
					list3.put("photo", foto);
					list3.put("status", "reserve");
					list3.put("player", player);
				}
				list3.put("team", list2);
				list3.put("team2", team);


				return new ModelAndView("editPlayer", "list3", list3);

			}
			return null;
		}else if(module.equalsIgnoreCase("captain")){


			if(action.equalsIgnoreCase("view")) {
				//				List<Player> list = playerDao.findCaptain(Integer.parseInt(teamId));
				List<TFootballTeam> list2 = teamDao.findAll();
				TFootballTeam team = teamDao.findById(Integer.parseInt(teamId));
				Map<String, Object> list3 = new HashMap<String, Object>();
				List<TPlayer> list = team.getCaptains();

				list3.put("team", list2);
				list3.put("player", list);
				list3.put("team2", team);

				return new ModelAndView("showCaptain", "list3", list3);
			}else if(action.equalsIgnoreCase("del")) {
				TCaptain captain = captainDao.findById(Integer.parseInt(playerId));
				captainDao.remove(captain);
				
				List<TFootballTeam> list2 = teamDao.findAll();
				TFootballTeam team = teamDao.findById(Integer.parseInt(teamId));
				Map<String, Object> list3 = new HashMap<String, Object>();
				List<TPlayer> list = team.getCaptains();

				list3.put("team", list2);
				list3.put("player", list);
				list3.put("team2", team);
				
				req.setAttribute("list3", list3);
				res.sendRedirect("main.htm?module=captain&action=view&teamid=" + teamId);
				
				return null;

			}else if(action.equalsIgnoreCase("add")) {
				
				List<TFootballTeam> list2 = teamDao.findAll();			
				
				TFootballTeam team = teamDao.findById(Integer.parseInt(teamId));
				
				List<TPlayer> captain = team.getCaptains();
				List<TPlayer> list = playerDao.findByProperty("TFootballTeam", team, 0);
				
				Iterator<TPlayer> i = captain.iterator();
				
				while(i.hasNext()) {
					list.remove(i.next());
				}
						
				Map<String, Object> list3 = new HashMap<String, Object>();

				list3.put("team", list2);
				list3.put("player", list);
				list3.put("team2", team);

				return new ModelAndView("newCaptain", "list3", list3);

			}
		}else if(module.equalsIgnoreCase("team")){
			if(action.equalsIgnoreCase("del")) {

				List<TFootballTeam> list2 = teamDao.findAll();
				Map<String, Object> list3 = new HashMap<String, Object>();
				TFootballTeam team = teamDao.findById(Integer.parseInt(teamId));
				teamDao.remove(team);

				list3.put("team", list2);

				req.setAttribute("list3", list3);
				res.sendRedirect("main.htm");

			}else if(action.equalsIgnoreCase("edit")) {

				Map<String, Object> list3 = new HashMap<String, Object>();
				TFootballTeam team = teamDao.findById(Integer.parseInt(teamId));

				list3.put("team2", team);

				return new ModelAndView("teamEdit", "list3", list3);

			}else{

				List<TFootballTeam> list2 = teamDao.findAll();
				Map<String, Object> list3 = new HashMap<String, Object>();
				TFootballTeam team = teamDao.findById(Integer.parseInt(teamId));
				List<TPlayer> playerList = playerDao.findByProperty("TFootballTeam", team, 0);

				Iterator i = playerList.iterator();
				List<Player> players = new ArrayList<Player>();
				List<Reserve> reserves = new ArrayList<Reserve>();

				while(i.hasNext()) {
					TPlayer tp = (TPlayer) i.next();
					if(tp instanceof Player){
						players.add((Player)tp);
					}else if(tp instanceof Reserve){
						reserves.add((Reserve)tp);
					}
				}

				list3.put("team", list2);
				list3.put("player", players);
				list3.put("reserve", reserves);
				list3.put("team2", team);

				return new ModelAndView("contact", "list3", list3);

			}
		}
		return null;

	}

}
